<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Admin;

use \App\All;
use Core\Cache\FileCache as FileCache;
use Model\Admin\MenuModel as MenuModel;

/**
 * Pes 后台公共类
 * @author lazyphp
 * @version 1.0
 */
class Common extends All {

    /**
     * 后台公共模块构造函数
     */
    public function __construct() {
        $this->is_login();
        $menuCache = new FileCache();
        $menuCache->createCache('menu', MenuModel::getMenu());
        $menu = $menuCache->loadCache('menu');
        $this->assign('menu', $menu);
        $this->checkNode($_SESSION['admin']['user']['user_group_id']);
    }

    /**
     * 获取登陆状态
     */
    public function is_login() {
        if (empty($_SESSION['pes_login'])) {
            header('Allow: POST');
            header('HTTP/1.1 405 Method Not Allowed');
            header('Content-Type: text/plain');
            exit;
        }
        if (empty($_SESSION['admin'])) {
            $this->error('您没权限打开真理之门!', '/');
        }
    }
    
    /**
     * 清除上传文件的记录
     */
    protected function clearUploadRecord(){
        foreach($_COOKIE as $key => $value){
            if(substr($key, 0, 6) == 'input_'){
                setcookie($key, NULL);
            }
        }
    }

    /**
     * 加载页眉
     */
    public function header() {
        $this->display('header');
    }

    /**
     * 加载页脚
     */
    public function footer() {
        $this->display('footer');
    }

    /**
     * 加载分页类
     * @return \Page
     */
    public function page() {
        require APP . 'expand/Page.class.php';
        $Page = new Page();
        return $Page;
    }

    /**
     * 上传方法
     */
    public function upload() {
        //获取上传的类型
        if (empty($_POST['type'])) {
            $data['status'] = 0;
            $data['info'] = '缺失上传类型';
            exit(json_encode($data));
        }
        $typeArray = array('img', 'file');
        $type = trim($_POST['type']);
        if (!in_array($type, $typeArray)) {
            $data['status'] = 0;
            $data['info'] = '错误的上传类型';
            exit(json_encode($data));
        }
        //对应的上传类型操作
        switch ($type) {
            case 'img':
                $fileType = array('jpg', 'jpge', 'bmp', 'gif', 'png');
                $fileTypeMes = '请上传图片格式的文件';
                $fileSizeMes = '图片大于5M，请压缩后再上传';
                $fileSavePath = './upload/img';
                break;
            case 'file':
                $fileType = array('zip', 'rar', '7z', 'doc', 'docx', 'pdf', 'xls', 'xlsx', 'ppt', 'pptx', 'txt');
                $fileTypeMes = '请上传以下格式的文件:' . implode(',', $fileType);
                $fileSizeMes = '文件大于5M，请压缩后再上传';
                $fileSavePath = './upload/file';
                break;
        }

        if (empty($_FILES['Filedata']['name'])) {
            $data['status'] = 0;
            $data['info'] = '请选择您要上传的文件';
            exit(json_encode($data));
        }

        $uploadFileType = pathinfo($_FILES['Filedata']['name']);
        //判断上传的文件是否得到许可
        if (!in_array($uploadFileType['extension'], $fileType)) {
            $data['status'] = 0;
            $data['info'] = $fileTypeMes;
            exit(json_encode($data));
        }
        //此处可扩展上传文件大小
        if ($_FILES['Filedata']['size'] > 5242800) {
            $data['status'] = 0;
            $data['info'] = $fileSizeMes;
            exit(json_encode($data));
        }
        //存储名
        $saveFileName = uniqid() . '.' . $uploadFileType['extension'];
        //存储路径
        $savePath = $fileSavePath . date('/Y-m-d/');
        //获取图片目录是否存在
        if (is_dir($fileSavePath) === false) {
            mkdir($fileSavePath);
        }
        //创建以日期形式存储的图片目录
        if (is_dir($fileSavePath . date('/Y-m-d')) === false) {
            mkdir($fileSavePath . date('/Y-m-d'));
        }

        /*
         * 此处可以再加一个水印扩展
         */

        //存储图片
        move_uploaded_file($_FILES["Filedata"]["tmp_name"], $savePath . $saveFileName);

        $data['status'] = 1;
        $data['info'] = array("上传成功", substr($savePath . $saveFileName, 1, strlen($savePath . $saveFileName)));
        exit(json_encode($data));
    }

}